#ifndef __POJHANDLER_HEADER
#define __POJHANDLER_HEADER

#include "poj.h"
#include "poj_task.h"
#include "poj_parser.h"
#include "mycurl.h"
#include <string>
#include <pthread.h>

class PojHandler {
private:
	typedef key_t msg_key;
	typedef pthread_mutex_t mutex;
private:
	static std::string login_data(const char *user_id, const char *passwd);
	static std::string submit_data(const char *problem_id, const char *language, const char *src);
	static std::string find_language_id(const char *language);
	static size_t save_http_contents(char *ptr, size_t size, size_t nmemb, void *user_data);

private:
	MyCurl 		*mycurl;
	PojTask 	current_task;
	PojParser 	parse_html;
	std::string current_result;
	int 		out_pipe;
	int 		msg_id;
	mutex*		out_pipe_mutex_ptr;

public:
	PojHandler(int curlID, std::string cookie_dir, int out_pipe, int msgid, pthread_mutex_t *mutex_ptr);
	~PojHandler();
	int login(const char *user_id, const char *passwd, void *user_data = NULL);
	int submit(PojTask &task);
	void get_judge_result();
	int send_result_to_listen();
};

#endif // __POJHANDLER_HEADER
